﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;

namespace Business
{
    public class BuMaster
    {
        public static string[] selCompanyRegister()
        {
            int sumHour = 0;
            int sumMinute = 0;
            string[] result = new string[3];
            DaMaster master = new DaMaster();
            DataTable tmpCompany = master.selCompanyRegister();
            result[0] += "<table cellspacing=\"0\"><thead><tr><td>STT</td><td>Tên Cục, Vụ, Viện</td><td>Tên viết tắt</td><td>Địa chỉ</td><td>Thời gian đăng ký</td><td>Số cán bộ</td></tr></thead><tbody>";

            int count = 1;
            int numOfRow = tmpCompany.Rows.Count;
            string companyID = String.Empty;
            if (numOfRow > 0)
            {
                // Duyet cac hang cua datatable
                for (int i = 0; i < numOfRow; i++)
                {
                    result[0] += "<tr";
                    if (count % 2 == 0)
                        result[0] += " class=\"alt\">";
                    else
                        result[0] += ">";
                    result[0] += "<td style=\"text-align: center;\">"+count+"</td>";
                    result[0] += "<td>" + tmpCompany.Rows[i]["CompanyName"] + "</td>";
                    result[0] += "<td style=\"text-align: center;\">" + tmpCompany.Rows[i]["CompanyTrend"] + "</td>";
                    result[0] += "<td>" + tmpCompany.Rows[i]["CompanyAddress"] + "</td>";
                    companyID = tmpCompany.Rows[i]["CompanyID"].ToString();
                    
                    DataTable time = master.selCompanyTimeRegister(companyID);
                    DataTable numOfStaff = master.selNumOfStaff(companyID);
                    int minute = Int32.Parse(time.Rows[0]["TimeMinute"].ToString());
                    int hour = 0;
                    if (minute >= 60)
                    {
                        int addHour = minute / 60;
                        minute -= addHour * 60;
                        hour = Int32.Parse(time.Rows[0]["TimeHour"].ToString()) + addHour;
                    }
                    result[0] += "<td style=\"text-align: center;\">" + hour + "," + minute + "</td>";
                    result[0] += "<td style=\"text-align: center;\">" + numOfStaff.Rows[0]["NumOfStaff"].ToString() + "</td></tr>";
                    sumHour += hour;
                    sumMinute += minute;
                    count++;
                }

            }
            result[0] += "</tbody></table>";
            result[1] += sumHour+" giờ : "+sumMinute+" phút";
            result[2] += (count - 1).ToString();
            return result;
        }


    }
}
